package com.pixiv.service.Impl;

import com.pixiv.DAO.RegisterLoginStateMapping;
import com.pixiv.Entity.RegisterPicture;
import com.pixiv.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;

@Service("registerBackPictureService")
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private RegisterLoginStateMapping registerLoginStateMapping;

    @Override
    public String getPictureInitName() {
        int size = registerLoginStateMapping.getSize();
        if (size > 0) {
            int picID = (int) (size * Math.random());

            String nameByPictureID = registerLoginStateMapping.getPictureByHang(picID);
            return nameByPictureID;
        }else
        {
            return "position/006BFMdqgy1gli0bv0ufwj31dk0qt1ky.jpg";
        }
    }

    @Override
    public String getPicture(int id) {
        String nameByPictureID = registerLoginStateMapping.getNameByPictureID(id);
        return nameByPictureID ;
    }

    public void registerUserByLoginParameter(String name,String pwd)
    {
        String pwdMd5 = DigestUtils.md5DigestAsHex(pwd.getBytes());
        registerLoginStateMapping.addUserWithLoginParameter(name,pwdMd5);
    }

    @Override
    public ArrayList<RegisterPicture> getPictures(Object picid) {
        return registerLoginStateMapping.getPictures(picid);
    }

    @Override
    public void deletePicture(int id) {
        registerLoginStateMapping.deletePicture(id);
    }

    @Override
    public void appendPicture(String path) {
        int picID = (int) (10000 * Math.random());
        registerLoginStateMapping.insertPicture(picID,path);


    }

}
